﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Fallin.Helpers;
using System.IO;
using System.Threading;
using Fallin.Entities;
using System.Runtime.Serialization.Formatters.Binary;

namespace Fallin.Initiator
{
    public partial class Initiator : Form
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Initiator));

        delegate void SetProgressDelegate(string progress);
        Thread thread;
        long numberOfSubFolders;
        DateTime startTime;
        IDictionary<string, FolderInfoEntity> dictionary;

        public Initiator()
        {
            InitializeComponent();
        }

        private void Initiator_Load(object sender, EventArgs e)
        {
            dictionary = new Dictionary<string, FolderInfoEntity>();
            startTime = DateTime.Now;
            thread = new Thread(new ThreadStart(CalculateAllDrive));
            thread.Start();
        }

        private void CalculateAllDrive()
        {
            DriveInfo[] driveInfos = DriveInfo.GetDrives();
            foreach (var drive in driveInfos)
            {
                if (drive.DriveType == DriveType.Fixed)
                {
                    DirectoryInfo[] directories = drive.RootDirectory.GetDirectories();

                    foreach (var directory in directories)
                    {
                        FileInfoHelper helper = new FileInfoHelper(directory.FullName);
                        helper.Progress += new EventHandler<Fallin.Events.ProgressEventArgs>(helper_Progress);
                        helper.SubFolderComplete += new EventHandler<Fallin.Events.FolderInfoEventArgs>(helper_SubFolderComplete);
                        helper.Run();
                        helper.Progress -= new EventHandler<Fallin.Events.ProgressEventArgs>(helper_Progress);
                        helper.SubFolderComplete -= new EventHandler<Fallin.Events.FolderInfoEventArgs>(helper_SubFolderComplete);
                    }
                }
            }

            DataBaseHelper.SaveData(dictionary);
        }

        private void SetProgress(string progress)
        {
            if (infoText.InvokeRequired)
            {
                SetProgressDelegate d = new SetProgressDelegate(SetProgress);
                Invoke(d, new object[] { progress });
            }
            else
            {
                numberOfSubFolders++;
                TimeSpan elapsed = DateTime.Now - startTime;

                infoText.Text = string.Format("{0} - {1}", numberOfSubFolders.ToString("##,#"), progress);
            }
        }

        void helper_SubFolderComplete(object sender, Events.FolderInfoEventArgs e)
        {
            dictionary[e.FolderInfo.FolderName] = e.FolderInfo;
        }

        void helper_Progress(object sender, Events.ProgressEventArgs e)
        {
            SetProgress(e.CurrentProgress);
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to stop current progress?",
                "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                thread.Abort();
                cancelButton.Enabled = false;
            }
        }

        private void Initiator_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (thread.IsAlive)
            {
                e.Cancel = true;
                WindowState = FormWindowState.Minimized;
            }
        }
    }
}
